package com.tigercel.xiangchengyun.ui.activities

import android.os.Message
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.ActivityCompat
import android.util.Log
import com.tigercel.xiangchengyun.AppConfig
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.common.MsgID
import com.tigercel.xiangchengyun.ui.BaseActivity
import com.tigercel.xiangchengyun.ui.fragments.FragmentController
import com.tigercel.xiangchengyun.utils.BottomNavigationViewHelper
import com.tigercel.xiangchengyun.utils.PermissionUtils
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File

class MainActivity : BaseActivity(), ActivityCompat.OnRequestPermissionsResultCallback {
    override val mContentLayoutResouredId: Int = R.layout.activity_main

    override fun init() {
        PermissionUtils.requestMultiPermissions(this, mPermissionGrant)
    }

    private val mPermissionGrant = PermissionUtils.PermissionGrant { requestCode ->
        when (requestCode) {
            PermissionUtils.CODE_MULTI_PERMISSION -> {
                val cacheFolder = File(AppConfig.CACHE_PATH)
                if (!cacheFolder.exists()) {
                    cacheFolder.mkdirs()
                }
            }
            else -> {
            }
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant)
    }

    override fun initViews() {
        BottomNavigationViewHelper.disableShiftMode(navigation)
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

        controller = FragmentController.getInstance(this, R.id.fl_content)
        controller?.showFragment(0)
    }

    override fun handleMsg(msg: Message) {
        when (msg.what) {
            MsgID.NEWS_CHANNEL_CHANGED -> {
                Log.e("======", "===========")
            }
        }
    }

    private var controller: FragmentController? = null

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_hot_news -> {

                controller?.showFragment(0)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_video -> {

                controller?.showFragment(1)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_report_material -> {

                controller?.showFragment(2)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_service -> {
                controller?.showFragment(3)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_me -> {
                controller?.showFragment(4)
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

}
